<!DOCTYPE html>
<html lang="zh">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
<<<<<<< HEAD
    <style>
        * {
            padding: 0;
            margin: 0;
            list-style: none;
        }

        .box {
            width: 300px;
            height: 200px;
            border: 1px solid #000;
            margin: auto;
        }

        .box>.head {
            display: flex;
            justify-content: space-between;
        }

        .box>.head>li {
            flex: 1;
            text-align: center;
            cursor: pointer;
            /* cursor: pointer; */
        }

        .active {
            background-color: #87ceeb;
            color: #fff;
        }

        .content>.main {
            display: none;
        }
    </style>
</head>

<body>
    <div class="box">
        <ul class="head">
            <li>标题一</li>
            <li>标题二</li>
            <li>标题三</li>
        </ul>

        <div class="content">
            <div class="main">内容一</div>
            <div class="main">内容二</div>
            <div class="main">内容三</div>
        </div>
    </div>

    <script>

        function PT() {
            let list = document.querySelectorAll("li");
            let main = document.querySelector(".main");
            //let lastIndex = 0; // 设置上一步操作的下标
            list[0].className = "active";
            main[0].style.display = "block";
            for (let i = 0; i < list.length; i++) {

                list[i].onclick = function () {
                    for (let n = 0; n < list.length; n++) {
                        list[n].className = " ";
                        main.style.display = "none";
                    }
                    this.className = "active";
                    main[i].style.display = "block";
                }
            }
        };
        PT();


        //  head容器每个li绑定事件
        // list.forEach(function (item, index) {
        //     item.onclick = function () {
        //         // li里面的active类名全部移除
        //         // list.forEach(function (item) {
        //         //     item.classList.remove("active");
        //         // });

        //         // 清除上一个操作的类名
        //         list[lastIndex].classList.remove("active");
        //         // 当前li操作添加类名
        //         item.classList.add("active");
        //         // 保存操作完毕之后的下标
        //         lastIndex = index;

        //         // 点击更换内容
        //         main.innerText = item.innerText;

        //         // 作业(不同的标题对应不同的容器)
        //     }
        // });
=======
</head>

<body>
    <button id="addBtn">添加一条新的访客信息</button>
    <button id="addDel">删除选中</button>
    <button id="addSort">年龄从大到小</button>
    <!-- 序列号	姓名	年龄	操作 -->

    <table border=1 cellspacing=0 cellpadding=10>
        <tbody>
            <tr>
                <th>序列号</th>
                <th>姓名</th>
                <th>年龄</th>
                <th>操作</th>
                <th>选择</th>
            </tr>

            <!--
            <tr>
                <td>1</td>
                <td>胡杭</td>
                <td>16</td>
                <td><button>删除</button></td>
            </tr>
            -->
        </tbody>
    </table>



    <script>
        let info = [{
            name: "胡杭",
            age: 16
        }, {
            name: "胜明",
            age: 22
        }, {
            name: "军毅",
            age: 21
        }, {
            name: "晓华",
            age: 13
        }, {
            name: "盛聪",
            age: 23
        }, {
            name: "侦剑",
            age: 32
        }, {
            name: "红翔",
            age: 25
        }, {
            name: "超维",
            age: 18
        }, {
            name: "士琪",
            age: 22
        }, {
            name: "艳华",
            age: 20
        }];

        let tbody = document.querySelector("tbody");
        let addBtn = document.getElementById("addBtn");
        let delBtn = tbody.querySelectorAll("button");
        // let delBtn = tbody.getElementsByTagName("button");
        let addDel = document.getElementById("addDel");

        let index = 0; // 模拟点击下标(每次自增1)   

        addBtn.onclick = function () {
            console.log(index);
            // 当前index值要小于数组长度,否则会报错，获取不到数组的值
            if (index < info.length) {
                // 创建新的tr
                let newTr = document.createElement("tr");
                // 新的tr添加tr单元格
                newTr.innerHTML = `
                <td>${index + 1}</td>
                <td>${info[index].name}</td>    
                <td>${info[index].age}</td>
                <td><button>删除</button></td>
                <td><input type="checkbox" class="ckb" ></td>
                `;

                //  将新的tr添加到tbody
                tbody.appendChild(newTr);

                // 删除单个信息
                delBtn = tbody.querySelectorAll("button");
                // console.log(delBtn);
                // 每个删除按钮绑定点击事件
                delBtn.forEach(function (el) {
                    el.onclick = function () {
                        // el == this
                        // console.log(this.parentElement.parentElement.remove());
                        el.parentElement.parentElement.remove();
                    }
                });
                // console.log("before", delBtn);
                // for (let i = 0; i < delBtn.length; i++) {
                //     delBtn[i].onclick = function () {
                //         delBtn[i].parentElement.parentElement.remove();
                //         console.log("after", delBtn);
                //     }
                // }

                // 添加完毕后自增
                index++;
            }
            let ckb = document.querySelectorAll(".ckb")

            addDel.onclick = function () {
                ckb.forEach(function (item) {

                    console.log(item);
                    console.log(item.checked);
                    item.parentNode.parentNode.remove();
                })
            }
        }


>>>>>>> origin/lzh
    </script>
</body>

</html>